package com.fw.config;

import com.fw.entity.MyException;
import com.fw.entity.Result;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionAdvice {
    @ResponseBody
    @ExceptionHandler(value = ArithmeticException.class)
    Result handleException(ArithmeticException e){
        e.printStackTrace();
        return new Result(null, "请求异常:"+ e.getMessage(), "000002");
    }

    @ResponseBody
    @ExceptionHandler(value = NullPointerException.class)
    Result handleException(NullPointerException e){
        e.printStackTrace();
        return new Result(null, "系统内部空指针错误", "000003");
    }

    @ResponseBody
    @ExceptionHandler(value = MyException.class)
    Result handleException(MyException e){

        return new Result(null, "异常信息:"+ e.getMessage(), e.getCode());
}

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    Result handleException(Exception e){
        e.printStackTrace();
        return new Result(null, "系统内部错误，Waterman太水了", "000001");
    }

    @ResponseBody
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    Result handleException(MissingServletRequestParameterException e){
        return new Result(null, "接口未填写参数", "010004");
    }

    @ResponseBody
    @ExceptionHandler(value = NumberFormatException.class)
    Result handleException(NumberFormatException e){
        return new Result(null, "数据格式错误", "010005");
    }

}
